package org.jeecgframework.core.common.controller;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import jeecg.business.entity.enter.EnterEntity;
import jeecg.system.pojo.base.TSUser;

import org.jeecgframework.core.common.model.common.SessionInfo;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.interceptors.DateConvertEditor;
import org.jeecgframework.core.util.ContextHolderUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;


/**
 * 基础控制器，其他控制器需集成此控制器获得initBinder自动转换的功能
 * 
 * 
 */
@Controller
@RequestMapping("/baseController")
public class BaseController {
	
	public final String [] COLORS = {"CC0000","00CC00","0000CC","99CC99","CCCC00","00CCCC","CC00CC","00CCBB"};
	
	/**
	 * 将前台传递过来的日期格式的字符串，自动转化为Date类型
	 * 
	 * @param binder
	 */
	@InitBinder
	public void initBinder(ServletRequestDataBinder binder) {
//		SimpleDateFormat dateFormat = new SimpleDateFormat(
//				"yyyy-MM-dd hh:mm:ss");
//		binder.registerCustomEditor(Date.class, new CustomDateEditor(
//				dateFormat, true));
		
		//-----update-begin---- author:zhangdaihao  date:20130227 for:时间转换问题
		binder.registerCustomEditor(Date.class, new DateConvertEditor());
		//-----update-end---- author:zhangdaihao  date:20130227 for:时间转换问题
	}
	
	public HttpSession getSession(){
		HttpSession session = ContextHolderUtils.getSession();		
		return session;
	}
	
	
	
	public String getWebPath(HttpServletRequest request){
		String contextPath = request.getScheme()+"://"+request.getServerName() +":"+request.getServerPort()+request.getContextPath()+"/";
		System.out.println(contextPath);
		return contextPath;
	}
	
	public String getPhyPath(HttpServletRequest request){
		return request.getSession().getServletContext().getRealPath("/");
	}
	
	public SessionInfo getSessionInfo(){
		HttpSession session = getSession();	
		SessionInfo si = (SessionInfo)session.getAttribute(Globals.USER_SESSION);	
		return si;
	}
	
	public TSUser getUser(){
		SessionInfo si = getSessionInfo();
		if(si == null) return null;
		return si.getUser();
	}
	
	public void setEnter(EnterEntity enter){
		HttpSession session = getSession();	
		session.setAttribute("enter" , enter);
	}
	
	public EnterEntity getEnter(){
		HttpSession session = getSession();	
		return (EnterEntity)session.getAttribute("enter");
	}
	
	public String getHomeIndex(){
		HttpSession session = getSession();	
		return (String)session.getAttribute("sindex");
	}
	
	public void setHomeIndex(String homeIndex){
		HttpSession session = getSession();	
		session.setAttribute("sindex", homeIndex);
	}
	
	public String getRoleCode(){
		SessionInfo si = getSessionInfo();
		return si.getRoleCode();
	}
	
	public boolean isSuperManage(){
		return getRoleCode().contains("admin");
	}
	

}
